package com.glzn.springboot03.util;

import java.io.UnsupportedEncodingException;
import java.util.Date;

/**
 * @author gongjq
 */
public class Frame {
	private static final int SERVER_WAVE = 0x01;
	private static final int SERVER_EVENT = 0x02;
	private static final int SERVER_PARA = 0x03;
	private static final int SERVER_REAL_DATA = 0x04;

	public static boolean isCurrentFrame(byte[] bytes, int frameCount){
		int curFramCount = bytes[5] << 8 | bytes[4];
		System.out.println("frameCount:"+frameCount+"curFramCount:"+curFramCount);
		return curFramCount == frameCount;
	}

	public static boolean isValidFrame(byte[] bytes, int framSize){
		byte cks = (byte) ByteUtil.getCks(bytes,framSize);
		System.out.println("calc cks="+ cks + ", byte[frameSize - 2]="+bytes[framSize - 2]);
		return bytes[framSize - 2] == cks;
	}

	public static int madeSendFrame(ComService comService, byte[] bytes, boolean reSend, int frameCount){
		int frameSize = 0;

		bytes[0] = 0x68;
		bytes[1] = 0;
		bytes[2] = 0;
		bytes[3] = 0x68;
		bytes[4] = (byte) frameCount;
		bytes[5] = (byte) (frameCount>>8);
		bytes[6] = (byte) comService.getCmd();
		if (comService.getServerType() == SERVER_EVENT){
			switch (comService.getCmd()){
				//获取装置类型
				case 0x00:
					//查询事件总览
				case 0x40:
					//查询当前定值区号
				case 0x20:
					//查询定值数目
				case 0x10:
					//查询系统定值数目
				case 0x11:
					frameSize = 9;
					bytes[1] = 3;
					break;
				//查询事件
				case 0x41:
					frameSize = 12;
					bytes[1] = 6;
					bytes[7] = comService.getEventType();
					bytes[8] = (byte) (comService.getEventStartIndex());
					bytes[9] = (byte) (comService.getEventStartIndex()>>8);
					break;
				default:
					break;
			}
		} else if(comService.getServerType() == SERVER_PARA){
			switch (comService.getCmd()){
				//获取装置类型
				case 0x00:
					//查询当前定值区号
				case 0x20:
					//查询定值数目
				case 0x10:
					//查询系统定值数目
					frameSize = 9;
					bytes[1] = 3;
					break;
				//查询定值
				case 0x11:
					frameSize = 12;
					bytes[1] = 6;
					bytes[7] = comService.getAreaNum();
					bytes[8] = (byte) (comService.getParaNum());
					bytes[9] = (byte) (comService.getParaNum()>>8);
					break;
				default:
					break;
			}
		} else if(comService.getServerType() == SERVER_WAVE){
			//查询录波总览
			if (((comService.getCmd() - 0x71) % 0x10) == 0){
				frameSize = 9;
				bytes[1] = 3;
			}
			switch (comService.getCmd()){
				//获取装置类型
				case 0x00:
					frameSize = 9;
					bytes[1] = 3;
					break;
				case 0x72:
					frameSize = 51;
					bytes[1] = 45;
					//发生时间+毫秒 8字节
					Date date = VeDate.strToDateLong(comService.getHappenTime());
					bytes[7] = (byte) (date.getYear() - 100);
					bytes[8] = (byte) (date.getMonth()+1);
					bytes[9] = (byte) (date.getDate());
					bytes[10] = (byte) (date.getHours());
					bytes[11] = (byte) (date.getMinutes());
					bytes[12] = (byte) (date.getSeconds());
					bytes[13] = (byte) (comService.getMillsecond());
					bytes[14] = (byte) (comService.getMillsecond()>>8);
					//类型 32字节
					String startType = comService.getStartType();
					byte[] gbks = new byte[0];
					try {
						gbks = startType.getBytes("GBK");
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
					System.arraycopy(gbks, 0, bytes, 15, gbks.length);
					bytes[47] = (byte) (comService.getPieceNum());
					bytes[48] = (byte) (comService.getPieceNum()>>8);
					break;
				default:
					break;
			}
		} else if (comService.getServerType() == SERVER_REAL_DATA) {
			if (comService.getCmd() == 0x50) {
				frameSize = 9;
				bytes[1] = 3;
			}
		}

		bytes[frameSize - 2] = (byte) ByteUtil.getCks(bytes, frameSize);
		bytes[frameSize - 1] = 0x16;

		return frameSize;
	}

}
